package com.thornbird.vehiclerecycling.model.record;

import com.thornbird.vehiclerecycling.model.record.property.RecordProperties;
import com.thornbird.vehiclerecycling.repository.UserRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageList<T> {

    public static final int DEFAULT_PAGE_SIZE = 10;

    private int current;

    private int total;

    private long totalRecords;

    private int size;

    private List<T> list = new ArrayList<>();

    private Map<String, List<String>> extraData = new HashMap<>();

    public <E> void setPageElements(Page<E> page) {
        setCurrent(page.getNumber());
        setTotal(page.getTotalPages());
        setTotalRecords(page.getTotalElements());
        setSize(page.getSize());
    }

    public static Pageable generatePageable() {
        return PageRequest.of(0, Integer.MAX_VALUE);
    }

    public static Pageable generatePageable(Integer pageNumber, Integer pageSize) {
        return PageRequest.of(pageNumber == null ? 0 : pageNumber,
                pageSize == null ? DEFAULT_PAGE_SIZE : Math.max(pageSize, 1));
    }

    public static Pageable generatePageable(Integer pageNumber, Integer pageSize, Sort sort) {
        return PageRequest.of(pageNumber == null ? 0 : pageNumber,
                pageSize == null ? DEFAULT_PAGE_SIZE : Math.max(pageSize, 1),
                sort);
    }

    public static <T extends Record, E> PageList<T> convertPage(Page<E> page, Class<T> clazz) {
        return convertPage(page, clazz, null, null);
    }

    public static <T extends Record, E> PageList<T> convertPage(Page<E> page, Class<T> clazz,
                                                                RecordProperties properties) {
        return convertPage(page, clazz, properties, null);
    }

    public static <T extends Record, E> PageList<T> convertPage(Page<E> page, Class<T> clazz,
                                                                RecordProperties properties,
                                                                UserRepository userRepository) {
        PageList<T> pageList = new PageList<T>();
        pageList.setPageElements(page);
        pageList.setList(Record.convertList(page.getContent(), clazz, properties, userRepository));
        return pageList;
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public long getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(long totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Map<String, List<String>> getExtraData() {
        return extraData;
    }

    public void setExtraData(Map<String, List<String>> extraData) {
        this.extraData = extraData;
    }

}
